// baseUrl1 = "http://192.168.137.107:8080";
baseUrl1 = "http://192.168.137.1:8080";
baseUrl2 = "http://192.168.137.1:8080";

function logout() {
    deleteCookie("login_phone");
    deleteCookie("token");
    display_login_info();
}

function display_login_info() {
    if (hasCookie("login_phone")) {
        document.querySelector("#login_signup_area").style["display"] = "none";
        document.querySelector("#logout_area").style["display"] = "flex";
    } else {
        document.querySelector("#login_signup_area").style["display"] = "flex";
        document.querySelector("#logout_area").style["display"] = "none";
        document.querySelector("#login_info").innerHTML = `用户：${getCookieValue("login_phone")}`;
    }
}

// 添加/更新数据
function setCookie(key, value, days = 1) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = key + "=" + encodeURIComponent(value) + expires + "; path=/";
}

// 删除数据
function deleteCookie(key) {
    setCookie(key, "", -1);
}

// 检查数据是否存在
function hasCookie(key) {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i].trim();
        if (cookie.startsWith(key + "=")) {
            return true;
        }
    }
    return false;
}

function getCookieValue(key) {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i].trim();
        if (cookie.startsWith(key + "=")) {
            return decodeURIComponent(cookie.substring(key.length + 1));
        }
    }
    return undefined;
}

function show_float_text(text) {
    document.querySelector("#text_float").innerHTML = text;
    document.querySelector("#float_area").style["display"] = "flex";
}


function float_area_close() {
    document.querySelector("#float_area").style["display"] = "none";
}

function limited_string(str, len) {
    if (str.length > len) {
        return str.substring(0, len) + "...";
    } else {
        return str;
    }
}
